$(document).ready(function() {
	$('#plugin-errors').hide();
	$('#plugin-errors').click(function(){
            $('#plugin-errors').hide();
	});
});

function disablePlugin(urlwebsite, pluginName) {
    $('.errors').hide();
    jQuery.ajax({
        type: "post",
        url: urlwebsite+'sys/backend_plugin/disableplugin/',
        data: {
            pluginName: pluginName
        },
        beforeSend: function() {
           $('#plugbutton_' + pluginName).text('Disabling...');
        },
        success: function(response) {
                $('#plugbutton_' + pluginName).text('Enable');
                $('#plugbutton_' + pluginName).replaceWith('<button id="plugbutton_'+ pluginName +'" class="formsubmit" style="font-weight:bold;width:100px;font-size:12px;" onclick="enablePlugin(\'' + urlwebsite + '\',\''+ pluginName +'\')">Enable</button>');
        }
    });
}

$('.plugin-errors').click(function()
{
	$('#plugin-errors').hide();
});

function enablePlugin(urlwebsite, pluginName)
{
	$('.errors').hide();
	jQuery.ajax({
		type: "post",
		url:''+urlwebsite+'sys/backend_plugin/enableplugin/',
                data: {
                    pluginName: pluginName
                },
                beforeSend: function() {
                   $('#plugbutton_' + pluginName).text('Enabling...');
                },
		success: function(response) {
			if(response =="errors in sql") {
                            $('#plugin-errors').show();
                            $('#installation-error').show();
			}
                        else if(response =="install.sql"){
                            $('#plugin-errors').show();
                            $('#installsql').show();
			}
                        else {
                           $('#plugbutton_' + pluginName).text('Disable');
                           $('#plugbutton_' + pluginName).replaceWith('<button id="plugbutton_'+ pluginName +'" class="formsubmit" style="font-weight:bold;width:100px;font-size:12px;" onclick="disablePlugin(\'' + urlwebsite + '\',\''+ pluginName +'\')">Disable</button>');
			}
		}
	});
}


function deletePlugin(urlwebsite, pluginName)
{
	$('.errors').hide();
	jQuery.ajax({
		type: "post",
		url:urlwebsite+'sys/backend_plugin/deleteplugin/',
		data: 'pluginName=' + pluginName,
		success: function(response) {
			if(response!='errors in sql')
			{
				$('#'+str_replace(" ","-",pluginName)+'-div').hide();
			}else
			{
				$('#plugin-errors').show();
				$('#uninstallation-error').show();
			}
		}
	});
}

function cachePlugin(pluginName)
{
	var urlwebsite = $('#hid-main-wsurl').val();
	jQuery.ajax({
		type: "post",
		url:''+urlwebsite+'sys/backend_ajax/cacheplugin/',
		data: 'plugin=' + pluginName,
		success: function(response) {
			if(response =='error')
			{
				alert('Plugin was not installed.');
			}
		}
	});


}


function str_replace (search, replace, subject)
{
	var result = "";
	var  oldi = 0;
	for (i = subject.indexOf (search); i > -1; i = subject.indexOf (search, i))
	{
		result += subject.substring (oldi, i);
		result += replace;
		i += search.length;
		oldi = i;
	}
	return result + subject.substring (oldi, subject.length);
}
